package com.our.sys.project1.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.web.UserSession;
import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.entity.Supplier;
import com.our.sys.project1.entity.SupplierBlacklist;
import com.our.sys.project1.entity.SupplierReview;
import com.our.sys.project1.entity.SupplierType;
import com.our.sys.project1.service.SupplierService;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    private static Logger logger = LoggerFactory.getLogger(SupplierController.class);

    @RequestMapping(value = "/query_type")
    public @ResponseBody
    Map<String, Object> queryType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<SupplierType> page = new Page<SupplierType>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("createTime");
            page.setOrder(Page.DESC);
        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        //UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name", name.trim());
            filters.add(filter);
        }

        Page<SupplierType> data = supplierService.queryType(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query")
    public @ResponseBody
    Map<String, Object> query(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Supplier> page = new Page<Supplier>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("createTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        //UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name_OR_py_OR_wb", name.trim());
            filters.add(filter);
        }

        Page<Supplier> data = supplierService.query(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_enable")
    public @ResponseBody
    Map<String, Object> queryEnable(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Supplier> page = new Page<Supplier>(request);
        String name = request.getParameter("name");
        Page<Supplier> data = supplierService.queryEnable(page, name);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_blacklist")
    public @ResponseBody
    Map<String, Object> queryBlacklist(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<SupplierBlacklist> page = new Page<SupplierBlacklist>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("createTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        //UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name_OR_py_OR_wb", name.trim());
            filters.add(filter);
        }

        Page<SupplierBlacklist> data = supplierService.queryBlack(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_app")
    public @ResponseBody
    Map<String, Object> queryApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<SupplierReview> page = new Page<SupplierReview>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("appTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");

        String supplier = request.getParameter("supplier");
        if (StringUtils.isNotBlank(supplier)) {
            PropertyFilter filter = new PropertyFilter("LIKES_supplier_OR_py_OR_wb", supplier.trim());
            filters.add(filter);
        }
        PropertyFilter filter = new PropertyFilter("EQL_appAccountId", u.getAccount().getId().toString());
        filters.add(filter);
        Page<SupplierReview> data = supplierService.queryReview(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_review")
    public @ResponseBody
    Map<String, Object> queryReview(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<SupplierReview> page = new Page<SupplierReview>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("appTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");

        String supplier = request.getParameter("supplier");
        if (StringUtils.isNotBlank(supplier)) {
            PropertyFilter filter = new PropertyFilter("LIKES_supplier_OR_py_OR_wb", supplier.trim());
            filters.add(filter);
        }
        PropertyFilter filter = new PropertyFilter("EQL_checkAccountId", u.getAccount().getId().toString());
        filters.add(filter);
        Page<SupplierReview> data = supplierService.queryReview(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/save_type")
    public @ResponseBody
    Map<String, Object> saveType(SupplierType supplierType) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.saveType(supplierType);
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save")
    public @ResponseBody
    Map<String, Object> save(HttpServletRequest request, Supplier supplier) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        try {
            map = supplierService.save(supplier, u.getAccount());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_blacklist")
    public @ResponseBody
    Map<String, Object> saveBlacklist(SupplierBlacklist supplierBlacklist) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.saveSupplierBlacklist(supplierBlacklist);
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_app")
    public @ResponseBody
    Map<String, Object> saveApp(HttpServletRequest request, SupplierReview supplierReview) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        try {
            supplierService.saveApp(supplierReview, u.getAccount());
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_review")
    public @ResponseBody
    Map<String, Object> saveReview(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String appId = request.getParameter("id");
        String checkState = request.getParameter("checkState");
        String checkNote = request.getParameter("checkNote");
        try {
            supplierService.checkApp(Long.valueOf(appId), u.getAccount(), checkState, checkNote);
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete_type")
    public @ResponseBody
    Map<String, Object> deleteType(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.deleteType(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete")
    public @ResponseBody
    Map<String, Object> delete(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.delete(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete_blacklist")
    public @ResponseBody
    Map<String, Object> deleteBlacklist(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.deleteBlacklist(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete_app")
    public @ResponseBody
    Map<String, Object> deleteApp(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            supplierService.deleteApp(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
